<?php
/**
  * Mar 4, 2008
  *
  * Coded by: Ropu
  *
  */

require_once 'classes/dao/appDAO.php';
class App {
  protected $id=-1;
  protected $name;
  protected $hacker_id;
  protected $title;
  protected $description;
  protected $thumb;
  
  protected $versions = null;
  protected $rate = null;
  
  public function __construct($id=-1){
    if($id != -1) {
      $this->id = $id;
      $this->id = AppDAO::get($this);
      $this->getVersions();
    }
  }
  
/*
 * Getters
 */
  public function getId() {
    return $this->id;
  }
  
  public function getName() {
    return $this->name;
  }
  
  public function getHackerId() {
    return $this->hacker_id;
  }
  
  public function getTitle() {
    return $this->title;
  }
  
  public function getDescription() {
    return $this->description;
  }
  
  public function getThumb() {
    return $this->thumb;
  }
  
  public function getVersions() {
    if(is_null($this->versions)) {
      $this->versions = AppVersionDAO::getVersions($this);
    }
    return $this->versions;
  }
  
  public function getLastVersion() {
    return end($this->getVersions());
  }
  
  public function getRate() {
    if(is_null($this->rate)) {
      $this->rate = AppDAO::getRate($this);
    }
    return $this->rate;
  }
  
  /*
 * Setters
 */

  public function setName($name) {
    $this->name = $name;
  }
  
  public function setHackerId($hacker_id) {
    $this->hacker_id = $hacker_id;
  }
  
  public function setTitle($title) {
    $this->title = $title;
  }
  public function setDescription($description) {
    $this->description = $description;
  }
  
  public function setThumb($thumb) {
    $this->thumb = $thumb;
  }
  
  public function newVersion(AppVersion $av) {
    if($this->id != -1) {
	    $this->getVersions();
      AppVersionDAO::insert($av, $this); 
    }
    $this->versions[] = $av;
  }
  
  public function save() {
    if(is_null($this->versions)) {
      throw new Exception('Set at least one version before saving');
    }
    if($this->id == -1) {
      $this->id = AppDAO::insert($this);
	    foreach($this->versions as $av) {
	      AppVersionDAO::insert($av, $this);
	    }
    } else {
      $this->id = AppDAO::update($this);
    }
  
    return $this->id;
  }
  
}
?>